home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicRGBChooserPanel.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  246 lines

  1. /*
  2.  * @(#)BasicRGBChooserPanel.java    1.6 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.event.*;
  26. import com.sun.java.swing.plaf.ComponentUI;
  27. import com.sun.java.swing.plaf.ColorChooserUI;
  28. import java.awt.*;
  29. import java.awt.image.*;
  30. import java.awt.event.*;
  31. import java.beans.PropertyChangeEvent;
  32. import java.beans.PropertyChangeListener;
  33. import java.io.Serializable;
  34.  
  35.  
  36. /**
  37.  * The standard RGB chooser.
  38.  * <p>
  39.  * Warning: serialized objects of this class will not be compatible with
  40.  * future swing releases.  The current serialization support is appropriate 
  41.  * for short term storage or RMI between Swing1.0 applications.  It will
  42.  * not be possible to load serialized Swing1.0 objects with future releases
  43.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  44.  * baseline for the serialized form of Swing objects.
  45.  *
  46.  * @version 1.6 02/02/98
  47.  * @author Tom Santos
  48.  */
  49. public class BasicRGBChooserPanel extends ColorChooserPanel implements ChangeListener, Serializable {
  50.   protected Color color = Color.red;
  51.   protected JSlider redSlider;
  52.   protected JSlider greenSlider;
  53.   protected JSlider blueSlider;
  54.   protected JPanel resultColor;
  55.  
  56.   String redString = "Red";
  57.   String greenString = "Green";
  58.   String blueString = "Blue";
  59.  
  60.     public BasicRGBChooserPanel() {
  61.         super();
  62.     }
  63.  
  64.     public BasicRGBChooserPanel( Color startColor ) {
  65.         super();
  66.     color = startColor;
  67.     }
  68.  
  69.  
  70.     public void setColor( Color newColor ) {
  71.     redSlider.setValue( newColor.getRed() );
  72.     greenSlider.setValue( newColor.getGreen() );
  73.     blueSlider.setValue( newColor.getBlue() );
  74.     }
  75.  
  76.     public Color getColor() {
  77.         return color;
  78.     }
  79.        
  80.     /**
  81.      * The background color, foreground color, and font are already set to the
  82.      * defaults from the defaults table before this method is called.
  83.      */                                    
  84.     public void installChooserPanel() {
  85.       //    addPropertyChangeListener( this );
  86.     
  87.  
  88.     setLayout( new BorderLayout() );
  89.  
  90.     // The box that displays the current color
  91.     resultColor = new JPanel () { public Dimension getPreferredSize() {
  92.                                              return new Dimension( 30, 45 ); } };
  93.     JPanel colorBuffer = new JPanel( );
  94.     colorBuffer.add(resultColor);
  95.  
  96.     resultColor.setBackground( color );
  97.     add( resultColor, BorderLayout.CENTER );
  98.  
  99.     // The panel that holds the sliders
  100.     JPanel sliderPanel = new JPanel();
  101.     sliderPanel.setLayout( new ColumnLayout() );
  102.     add( sliderPanel, BorderLayout.WEST );
  103.     //    sliderPanel.setBorder(new LineBorder(Color.black));
  104.  
  105.     // The slider for the red value
  106.     redSlider = new JSlider( JSlider.HORIZONTAL, 0, 255, color.getRed() );
  107.     redSlider.setMajorTickSpacing( 85 );
  108.     redSlider.setMinorTickSpacing( 17 );
  109.     redSlider.setPaintTicks( true );
  110.     redSlider.setPaintLabels( true );
  111.  
  112.     // The panel that holds the components for the Red element
  113.     JPanel redPanel = new JPanel();
  114.     
  115.     JLabel rLabel = new JLabel("Red");
  116.     redPanel.add(rLabel);
  117.     redPanel.add( redSlider);
  118. /*    Spinner rSpin = new Spinner(color.getRed());
  119.     rSpin.setMaximum(255);
  120.     redPanel.add(rSpin);*/
  121.     sliderPanel.add( redPanel );
  122.  
  123.     // The slider for the green value
  124.     greenSlider = new JSlider( JSlider.HORIZONTAL, 0, 255, color.getRed() );
  125.     greenSlider.setMajorTickSpacing( 85 );
  126.     greenSlider.setMinorTickSpacing( 17 );
  127.     greenSlider.setPaintTicks( true );
  128.     greenSlider.setPaintLabels( true );
  129.  
  130.     // The panel that holds the components for the green element
  131.     JPanel greenPanel = new JPanel();
  132.  
  133.     JLabel gLabel = new JLabel("Green");
  134.     greenPanel.add(gLabel);
  135.     greenPanel.add( greenSlider );
  136.     sliderPanel.add( greenPanel );
  137.  
  138.     // The slider for the blue value
  139.     blueSlider = new JSlider( JSlider.HORIZONTAL, 0, 255, color.getRed() );
  140.     blueSlider.setMajorTickSpacing( 85 );
  141.     blueSlider.setMinorTickSpacing( 17 );
  142.     blueSlider.setPaintTicks( true );
  143.     blueSlider.setPaintLabels( true );
  144.  
  145.     // The panel that holds the components for the blue element
  146.     JPanel bluePanel = new JPanel();
  147.  
  148.     JLabel bLabel = new JLabel("Blue");
  149.     bluePanel.add(bLabel);
  150.     bluePanel.add( blueSlider );
  151.     //    bluePanel.setBorder(new LineBorder(Color.black));
  152.     sliderPanel.add( bluePanel );
  153.  
  154.     int labelWidth = Math.max(rLabel.getPreferredSize().width, gLabel.getPreferredSize().width);
  155.     labelWidth = Math.max(labelWidth, bLabel.getPreferredSize().width);
  156.     rLabel.setPreferredSize(new Dimension(labelWidth, rLabel.getPreferredSize().height));
  157.     gLabel.setPreferredSize(new Dimension(labelWidth, gLabel.getPreferredSize().height));
  158.     bLabel.setPreferredSize(new Dimension(labelWidth, bLabel.getPreferredSize().height));
  159.  
  160.     setPreferredSize( new Dimension( sliderPanel.getPreferredSize().width + resultColor.getPreferredSize().width, 
  161.                      sliderPanel.getPreferredSize().height + resultColor.getPreferredSize().height));
  162.     // Install listeners
  163.     redSlider.addChangeListener( this );
  164.     greenSlider.addChangeListener( this );
  165.     blueSlider.addChangeListener( this );
  166.     }
  167.  
  168.     public void uninstallChooserPanel() {
  169.       //removePropertyChangeListener( this );
  170.     color = null;
  171.     redSlider = null;
  172.     greenSlider = null;
  173.     blueSlider = null;
  174.     }
  175.  
  176.     public void stateChanged( ChangeEvent e ) {
  177.     if ( e.getSource() instanceof JSlider ) {
  178.         JSlider source = (JSlider)e.getSource();
  179.         Color oldColor = color;
  180.  
  181.         if ( source == redSlider ) {
  182.             color = new Color( redSlider.getValue(), color.getGreen(), color.getBlue() );
  183.         }
  184.         else if ( source == greenSlider ) {
  185.             color = new Color( color.getRed(), greenSlider.getValue(), color.getBlue() );
  186.         }
  187.         else if ( source == blueSlider ) {
  188.             color = new Color( color.getRed(), color.getGreen(), blueSlider.getValue() );
  189.         }
  190.  
  191.         resultColor.setBackground( color );
  192.         resultColor.repaint();
  193.  
  194.         fireColorPropertyChange( oldColor, color );
  195.     }
  196.     }
  197. }
  198.  
  199. class ColumnLayout implements LayoutManager {
  200.  
  201.   int xInset = 5;
  202.   int yInset = 5;
  203.   int yGap = 2;
  204.  
  205.   public void addLayoutComponent(String s, Component c) {}
  206.  
  207.   public void layoutContainer(Container c) {
  208.       Insets insets = c.getInsets();
  209.       int height = yInset + insets.top;
  210.       
  211.       Component[] children = c.getComponents();
  212.       Dimension compSize = null;
  213.       for (int i = 0; i < children.length; i++) {
  214.       compSize = children[i].getPreferredSize();
  215.       children[i].setSize(compSize.width, compSize.height);
  216.       children[i].setLocation( xInset + insets.left, height);
  217.       height += compSize.height + yGap;
  218.       }
  219.  
  220.   }
  221.  
  222.   public Dimension minimumLayoutSize(Container c) {
  223.       Insets insets = c.getInsets();
  224.       int height = yInset + insets.top;
  225.       int width = 0 + insets.left + insets.right;
  226.       
  227.       Component[] children = c.getComponents();
  228.       Dimension compSize = null;
  229.       for (int i = 0; i < children.length; i++) {
  230.       compSize = children[i].getPreferredSize();
  231.       
  232.       height += compSize.height + yGap;
  233.       width = Math.max(width, compSize.width + insets.left + insets.right + xInset*2);
  234.       }
  235.       height += insets.bottom;
  236.       return new Dimension( width, height);
  237.   }
  238.   
  239.   public Dimension preferredLayoutSize(Container c) {
  240.       return minimumLayoutSize(c);
  241.   }
  242.    
  243.   public void removeLayoutComponent(Component c) {}
  244.  
  245. }
  246.